package com.medical.goods.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.medical.goods.entity.CommodityEntity;
import com.medical.goods.service.CommodityService;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-08-13 20:42:37
 */
@RestController
@RequestMapping("goods/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;


    //查询商品信息
    @RequestMapping("/commodityList")
    public List<CommodityEntity> commodityList(){
        return commodityService.list();
    }


    /*
     * 根据商品分类Id查看商品信息*/
    @RequestMapping("/check")
    public List<CommodityEntity> check(@RequestParam(value = "tid",defaultValue = "1")Integer tid,
                                       @RequestParam(value = "name",defaultValue = "")String name
    ){
        return commodityService.check(tid,name);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = commodityService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据商品id回显商品信息
     *
     */
    @RequestMapping("/echo/{id}")
    public R echo(@PathVariable("id")Integer id){
        CommodityEntity commodity = commodityService.getById(id);

        return R.ok().put("commodity",commodity);
    }



    /**
     * 根据商品分类tid查询
     */
    @RequestMapping("/info/{tid}")
    public R info(@PathVariable("tid") Integer tid){
		CommodityEntity commodity = commodityService.getOne(new QueryWrapper<CommodityEntity>().eq("tid",tid));

        return R.ok().put("commodity", commodity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CommodityEntity commodity){
		commodityService.add(commodity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CommodityEntity commodity){
		commodityService.updates(commodity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable("id")Integer id){
		commodityService.delete(id);

        return R.ok();
    }

}
